
$(function(){
	//$.messager.progress();
	loadTopMenu();
	tabClose();
	tabCloseEven();
	//$.messager.progress('close');
});

//判断是否具备权限
function isAuth(key){
	if(_keys.indexOf(','+key+',')>-1){
		return false;
	}
	return true;
}

//加载模块
function loadTopMenu(){
	var apptopMenu= $("#app-topMenu");
    apptopMenu.empty();
	var menulist = "";
    $.each(_moduals, function(i, n) {
		if(isAuth(n.key)){
			menulist +='<div title="'+n.name+'" key="'+n.key+'" data-options="iconCls:\'icon '+n.icon+'\'"></div>';
		}
    });
	apptopMenu.append(menulist);
	apptopMenu.tabs({
		onSelect:function(title,index){
			var tab = apptopMenu.tabs('getSelected');
			var key = tab.context.attributes.key.value;
			loadLeftMenu(key);
		}
	});
}
//加载左侧菜单
function loadLeftMenu(modualKey) {
	var appLeftMenu= $("#app-leftMenu");
	var len = appLeftMenu.accordion("panels").length;
	//清除原有的菜单
	while(len>0){
		appLeftMenu.accordion("remove",0);
		len--;
	}
	var menus = _menus[modualKey];
    $.each(menus, function(i, n) {
		if(!isAuth(n.key)){
			return true;
		}
		var menulist = '<ul>';
        $.each(n.menus, function(j, o) {
			if(isAuth(o.key)){
				menulist += '<li><div><a href="javascript:;" url="' + o.url + '" ><span class="icon icon-width '+o.icon+'" ></span>' + o.name + '</a></div></li> ';
			}
        });
		menulist += '</ul>';
		appLeftMenu.accordion('add', {
			title: n.name,
			iconCls:'icon '+n.icon,
			content: menulist,
			selected: i==0?true:false
		});
    });
	appLeftMenu.find('li a').click(function(){
		var tabTitle = $(this).text();
		var url = $(this).attr("url");
		var icon = $(this).attr("class");
		appLoadTab(tabTitle,url,icon);
		appLeftMenu.find('li div').removeClass("selected");
		$(this).parent().addClass("selected");
	}).hover(function(){
		$(this).parent().addClass("hover");
	},function(){
		$(this).parent().removeClass("hover");
	});
	//appLeftMenu.accordion();
}

function appLoadTab(subtitle,url,icon){
	if(!$('#appTabs').tabs('exists',subtitle)){
		var tabOpts = {
			title:subtitle,
			cache:true,
			fit:true,
			iconCls:'icon-mini-refresh',
			closable:true
		}
		if(url.indexOf("http://")==0){
			tabOpts.content = createFrame(url);
		}else{
			tabOpts.href=url;
		}
		$('#appTabs').tabs('add',tabOpts);
	}else{
		$('#appTabs').tabs('select',subtitle);
	}
	tabClose();
}


function createFrame(url)
{
	var s = '<iframe name="mainFrame" scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
	return s;
}


function tabClose()
{
	/*双击关闭TAB选项卡*/
	var appTabs = $("div#appTabs");
	appTabs.find(".tabs-inner").dblclick(function(){
		var subtitle = $(this).children("span").text();
		$('#appTabs').tabs('close',subtitle);
	}).bind('contextmenu',function(e){
		var  tabContextMenu = $('div#tabContextMenu');
		tabContextMenu.menu('show', {
			left: e.pageX,
			top: e.pageY,
		});
		
		var subtitle =$(this).children("span").text();
		tabContextMenu.data("currtab",subtitle);
		return false;
	});
}
//绑定右键菜单事件
function tabCloseEven()
{
	//关闭当前
	$('#mm-tabclose').click(function(){
		var currtab_title = $('#tabContextMenu').data("currtab");
		$('#appTabs').tabs('close',currtab_title);
	})
	//全部关闭
	$('#mm-tabcloseall').click(function(){
		$('.tabs-inner span').each(function(i,n){
			var t = $(n).text();
			$('#appTabs').tabs('close',t);
		});	
	});
	//关闭除当前之外的TAB
	$('#mm-tabcloseother').click(function(){
		var currtab_title = $('#tabContextMenu').data("currtab");
		$('.tabs-inner span').each(function(i,n){
			var t = $(n).text();
			if(t!=currtab_title)
				$('#appTabs').tabs('close',t);
		});	
	});
	//关闭当前右侧的TAB
	$('#mm-tabcloseright').click(function(){
		var nextall = $('.tabs-selected').nextAll();
		if(nextall.length==0){
			//msgShow('系统提示','后边没有啦~~','error');
			alert('后边没有啦~~');
			return false;
		}
		nextall.each(function(i,n){
			var t=$('a:eq(0) span',$(n)).text();
			$('#appTabs').tabs('close',t);
		});
		return false;
	});
	//关闭当前左侧的TAB
	$('#mm-tabcloseleft').click(function(){
		var prevall = $('.tabs-selected').prevAll();
		if(prevall.length==0){
			alert('到头了，前边没有啦~~');
			return false;
		}
		prevall.each(function(i,n){
			var t=$('a:eq(0) span',$(n)).text();
			$('#appTabs').tabs('close',t);
		});
		return false;
	});
	
	var appTop = $("#app-topMenu");
}
